package server.command.handle.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import printfileformat.Print;
import server.command.handle.ServerCommandHandler;
import server.help.HelpContext;

public class CommandHandler implements ServerCommandHandler {

	@Override
	public void command() throws IOException {
		// TODO Auto-generated method stub
		//open port
		String localPath="server";
		ServerSocket ss=new ServerSocket(8887);
		Socket acceptS=ss.accept();  //open port action
		System.out.println("server connection............");
		OutputStream os=acceptS.getOutputStream();
		InputStream is=acceptS.getInputStream();
		BufferedReader bufR=new BufferedReader(new InputStreamReader(is));
		os.write(HelpContext.helpDocument().toString().getBytes());
		String commandRecever="";
		String fPath="";
		while((commandRecever=bufR.readLine())!=null)
		{
			//commandKind : CP
			if(commandRecever.equals("cp"))
			{
				this.handleCP(fPath, bufR, os);
			}
			//commandKind : SCP
			else if(commandRecever.equals("scp"))//commandRecever.equals("SCP")
			{
				this.handleSCP(fPath,bufR,os);
				
			}
			else if(commandRecever.equals("ls"))//commandRecever.equals("ls")
			{
				this.handleLS(fPath,bufR,os);
				
			}
			else if(commandRecever.equals("rm"))//commandRecever.equals("SCP")
			{
				this.handleRM(fPath,bufR,os);
			}
			else if(commandRecever.equals("quit"))//commandRecever.equals("SCP")
			{
				this.handleSCP(fPath,bufR,os);
				
			}
			else{
				os.write(HelpContext.helpDocument().toString().getBytes());
			}
				
		}
	}

	@Override
	public void handleCP(String path, BufferedReader bufR, OutputStream os)
			throws IOException {
		// TODO Auto-generated method stub
		//read fPath
		path=bufR.readLine();
		System.out.println("client want file: "+path);
		//catch server local file and delivery
		InputStream fileInput = null;
//		FileOutputStream fileOutput = new FileOutputStream(fPath);
		try {
			fileInput = new FileInputStream(path);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			os.write("not found file\n".getBytes());
			System.out.println("file not fount: "+path);
		}
		os.write("file found\n\r".getBytes());
		byte[] buf=new byte[fileInput.available()];
		fileInput.read(buf);
		os.write(buf);
		os.write("file end\n\r".getBytes());
		fileInput.close();  //release temporary InputStream
		
	}

	@Override
	public void handleSCP(String path, BufferedReader bufR, OutputStream os) throws IOException {
		// TODO Auto-generated method stub
		 OutputStream fileOutput = null;
			try {
				fileOutput = new FileOutputStream("copy.txt");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 System.out.println("file creat success");
			 String recever="";
			 while((recever=bufR.readLine())!=null)
			 {
				 System.out.println(recever);
				 if(recever.equals("file end"))
					 break;
				 System.out.println(recever);
				 fileOutput.write(recever.getBytes());
				 fileOutput.write("\n\r".getBytes());
			 }
			 fileOutput.close();
		
	}

	@Override
	public void handleLS(String path, BufferedReader bufR, OutputStream os) throws IOException {
		// TODO Auto-generated method stub
		Print p=new Print();
		p.readFile(path);
		os.write(p.getSb().toString().getBytes());
	}

	@Override
	public void handleRM(String path, BufferedReader bufR, OutputStream os) throws IOException {
		// TODO Auto-generated method stub
		// 建立当前目录中文件的File对象
        File file = new File(path);
        // 取得代表目有文件的File对象数组
        StringBuilder sb=new StringBuilder();
        if(!file.exists())
        {
        	sb.append("the file you want find not exit");
            os.write(os.toString().getBytes());
        }
        else
        {
        	sb.append(path+"\n");
        	int fileLevel=0;
        	File temp=file;
        	while(temp.isDirectory())
        	{
        		File[] list = file.listFiles();
        		for(int i=0;i<list.length;i++)
        		{
        			
        		}
        		
        	}
        	
        }
}
        // 遍历file数组
        

	@Override
	public void handleQuit(String path) {
		// TODO Auto-generated method stub
		
	}


	

}
